Skip to content

NO TICKET: /geospatial refactor#275

Closed
jacob-a-brown wants to merge 1 commit into
stagingfrom
jab-geospatial-refactor
Closed

NO TICKET: /geospatial refactor#275
jacob-a-brown wants to merge 1 commit into
stagingfrom
jab-geospatial-refactor

Conversation

@jacob-a-brown

Copy link
Copy Markdown
Contributor

Only querying the thing object for geospatial should speed up the process because fewer queries/subqueries need to occur. Since eager loading is used to get the current location, the location data is already available on the thing object.

Why

This PR addresses the following problem / context:

  • The /geospatial endpoint has been too slow to load the map in Ocotillo

How

Implementation summary - the following was changed / added / removed:

  • To attempt to fix this speed issue I only query the thing table. Since the related tables are eagerly loaded getting the current_location and then making a GeoJSON FeatureClass should avoid the N+1 error. Previously subqueries were used, which could adversely effect the performance of the API.
    • We may want to consider a larger refactor of the API where we remove all eager loading and then for each endpoint we use selectin or joinedload to eagerly load the tables/relationships we want. This will, however, take a lot more time to implement everywhere. So, this may just be a temporary solution until we do that larger refactor.

Pros/Cons

Any special considerations, workarounds, or follow-up work to note?

  • Pros
    • This is a simple implementation that is easy to understand and maintain.
    • With eager loading N+1 problems should be avoided.
  • Cons
    • A bunch of related tables are eagerly loaded when the thing table is queried. This can reduce the performance of the API.

Only querying the thing object for geospatial should speed up the
process because fewer queries/subqueries need to occur. Since
eager loading is used to get the current location, the location
data is already available on the thing object.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread api/geospatial.py
Comment on lines +104 to +108
def make_feature_dict(thing):
current_location = thing.current_location
x = current_location.latlon[1]
y = current_location.latlon[0]
elevation = current_location.elevation

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Guard against missing current_location in geospatial features

make_feature_dict now dereferences thing.current_location and its latlon/elevation without checking for a valid location. When a thing has no active location association (current_location returns None, e.g., its latest association has effective_end set), this code raises before building the FeatureCollection, causing the /geospatial (and shapefile) endpoints to 500. Previously the query filtered to active locations (effective_end is NULL), so such records were skipped. Please short-circuit or filter out things without a current location before accessing coordinates.

Useful? React with 👍 / 👎.

@codecov-commenter

codecov-commenter commented Dec 5, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
api/geospatial.py 97.77% <100.00%> (+0.15%) ⬆️
services/geospatial_helper.py 66.66% <100.00%> (-7.81%) ⬇️

... and 25 files with indirect coverage changes

@jirhiker jirhiker closed this Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants